// pages/bind.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    linkTel:'',
    VerifyCode:'获取验证码',
    code_disable:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  blurTel: function (e) {
    var linkTel = e.detail.value.replace(/\s/g, "");
    this.setData({
      linkTel: linkTel
    })
  },
  setVerify:function(e) {
    if (this.data.code_disable){
        return;
    }
    var that = this
    var linkTel = this.data.linkTel;
    if (linkTel == '' || linkTel.length < 11) {
      wx.showModal({
        content: '请输入正确的手机号',
        showCancel: false,
      });
      return;
    }
    var total_micro_second = 60 * 1000;    //表示60秒倒计时，想要变长就把60修改更大
    //验证码倒计时
    count_down(this, total_micro_second);
    wx.request({
      url: getApp().data.servsers + 'customer/sendVcode',
      method: 'GET',
      data: {
        linkTel: linkTel,
        openid: wx.getStorageSync('user').openid
      },
      header: {
        'content-type': 'application/json',
        'Cookie': 'PHPSESSID=' + wx.getStorageSync('session_id')
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.success) {
          that.setData(
            {
              code_disable:true
            }
          )
          wx.showModal({
            title: '提示',
            content: '发送验证码成功！',
            showCancel: false
          })
        }else {
          wx.showModal({
            title: '提示',
            content: res.data.message,
            showCancel: false
          })
        }
      }
    });
  },

  formSubmit: function (e) {
    var formData = e.detail.value; 
    if (formData.linkTel == '' || formData.linkTel.length < 11) {
      wx.showModal({
        content: '请输入正确手机号',
        showCancel: false,
      });
      return;
    }
    if (formData.vcode == '') {
      wx.showModal({
        content: '请输入验证码',
        showCancel: false,
      });
      return;
    }
    formData.openid = wx.getStorageSync('user').openid;
    console.log(formData);
    wx.request({
      url: getApp().data.servsers + 'customer/registBind',
      method: 'POST',
      data: formData,
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'Cookie': 'PHPSESSID=' + wx.getStorageSync('session_id')
      },
      success: function (res) {

        console.log(res.data);
        if (res.data.success) {
          wx.showModal({
            title: '提示',
            content: "绑定成功!",
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.navigateBack();
              }
            }
          });
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.message,
            showCancel: false
          })
        }

      }
    })
  }
})

/* 毫秒级倒计时 */
function count_down(that, total_micro_second) {
  if (total_micro_second <= 0) {
    that.setData({
      VerifyCode: "重新发送",
      code_disable: false
    });
    // timeout则跳出递归
    return;
  }

  // 渲染倒计时时钟
  that.setData({
    VerifyCode: date_format(total_micro_second) + " 秒"
  });

  setTimeout(function () {
    // 放在最后--
    total_micro_second -= 10;
    count_down(that, total_micro_second);
  }, 10)



}

// 时间格式化输出，如03:25:19 86。每10ms都会调用一次
function date_format(micro_second) {
  // 秒数
  var second = Math.floor(micro_second / 1000);
  // 小时位
  var hr = Math.floor(second / 3600);
  // 分钟位
  var min = fill_zero_prefix(Math.floor((second - hr * 3600) / 60));
  // 秒位
  var sec = fill_zero_prefix((second - hr * 3600 - min * 60));// equal to => var sec = second % 60;
  // 毫秒位，保留2位
  var micro_sec = fill_zero_prefix(Math.floor((micro_second % 1000) / 10));

  return sec;
}

// 位数不足补零
function fill_zero_prefix(num) {
  return num < 10 ? "0" + num : num
}